package com.star.controller;


import com.star.domain.UserInfo;
import com.star.domain.UserLogin;
import com.star.dto.ResponseData;
import com.star.service.UserInfoService;
import com.star.service.UserLoginService;
import com.star.util.Base64Img;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 用户信息
 * </p>
 *
 * @author Star
 * @since 2021-04-21
 */
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserLoginService userLoginService;

    /**
     * 通用修改
     *
     * @return
     */
    @RequestMapping("/updateBase")
    private @ResponseBody
    ResponseData updateBase(UserInfo userInfo, UserLogin userLogin) {
        //进行修改
        Boolean flogin = userInfoService.updateUserName(userInfo);
        ResponseData responseData = ResponseData.ok();
        if (flogin) {
            // 用户信息 存储
            UserLogin login = userLoginService.LoginByLogId(userLogin);
            if (userInfo.getInfoName() != null) {//昵称
                login.getUserInfo().setInfoName(userInfo.getInfoName());
            } else if (userInfo.getInfoAge() != null) {//年龄
                login.getUserInfo().setInfoAge(userInfo.getInfoAge());
            } else if (userInfo.getInfoEmail() != null) {//邮箱
                login.getUserInfo().setInfoEmail(userInfo.getInfoEmail());
            } else if (userInfo.getInfoCard() != null&&userInfo.getInfoCard().length()==18) {//身份证
                login.getUserInfo().setInfoCard(userInfo.getInfoCard());
            } else if (userInfo.getInfoPhone() != null) {//电话
                login.getUserInfo().setInfoPhone(userInfo.getInfoPhone());
            } else if (userInfo.getInfoGender() != null) {//性别
                login.getUserInfo().setInfoGender(userInfo.getInfoGender());
            }else if (userInfo.getInfoSignature()!=null&&userInfo.getInfoSignature().length()<50){//个性签名
                login.getUserInfo().setInfoSignature(userInfo.getInfoSignature());
            }
            //存储
            responseData.putDataValue("userLogin", login);
        } else {
            responseData = ResponseData.customerError();
        }
        return responseData;
    }

    /**
     * 修改头像
     */
    @RequestMapping("/img")

    private @ResponseBody
    ResponseData uploadImage(UserInfo userInfo, UserLogin userLogin, String imgbase64) {
        //把base64图片格式转换
        MultipartFile multipartFile = Base64Img.base64ToMultipart(imgbase64);
        ResponseData responseData = ResponseData.ok();
        try {
            //进行修改
            String Addar = userInfoService.updateImg(userInfo, userLogin.getLogId(), multipartFile);
            if (Addar != null) {
                //查询
                // 用户信息 存储
                UserLogin login = userLoginService.LoginByLogId(userLogin);
                login.getUserInfo().setInfoHeadUrl(Addar);
                responseData.putDataValue("userLogin", login);
            } else {
                responseData = ResponseData.customerError();
            }
        } catch (Exception e) {
            responseData = ResponseData.customerError();
        }
        return responseData;
    }
}


